package org.light.domain;

import java.util.Iterator;
import java.util.Set;

import org.light.exception.ValidateException;
import org.light.utils.DomainUtil;
import org.light.utils.StringUtil;

public class DaoImpl extends Class {
	protected Domain domain;
	protected Dao dao;
	protected Type extendedType;
	
	public Type getExtendedType() {
		return extendedType;
	}


	public void setExtendedType(Type extendedType) {
		this.extendedType = extendedType;
	}


	public String generateDaoImplString() throws ValidateException{
		StringBuilder sb = new StringBuilder();
		sb.append("package ").append(this.domain.getDaoimplSuffix()+"\n\n");
		Set<String> imports = this.generateImportStrings();
		imports.add("database/sql");
		imports.add("fmt");
		imports.add("strconv");
		imports.add(". \"database/sql\"");
		
		if (StringUtil.isBlank(this.domain.getDbType())||"MariaDB".equalsIgnoreCase(this.domain.getDbType())||"MariaDB".equalsIgnoreCase(this.domain.getDbType())||"MySQL".equalsIgnoreCase(this.domain.getDbType())) {
			imports.add("_\"github.com/go-sql-driver/mysql\"");
		}else if ("PostgreSQL".equalsIgnoreCase(this.domain.getDbType())||"pgsql".equalsIgnoreCase(this.domain.getDbType())) {
			imports.add("_\"github.com/lib/pq\"");
		}else if ("Oracle".equalsIgnoreCase(this.domain.getDbType())) {
			imports.add("_\"github.com/godror/godror\"");
		}else {
			throw new ValidateException(this.domain.getDbType()+"数据库不被支持！");
		}
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()));
		imports.add(DomainUtil.packagetokenToGoPackageFolder(this.domain.getProjectName(),this.domain.getPackageToken()+".utils"));
		String importsStr = DomainUtil.gemerateImportStr(imports);
		sb.append(importsStr);
		
		Iterator it2 = this.methods.iterator();
		while(it2.hasNext()){
			Method m = (Method)it2.next();
			sb.append(m.generateMethodString()).append("\n");
		}
		
		return sb.toString();
	}


	public Domain getDomain() {
		return domain;
	}


	public void setDomain(Domain domain) {
		this.domain = domain;
	}
	
	public ValidateInfo validate(){
		ValidateInfo info = new ValidateInfo();
		info.setSuccess(true);
		return info;
	}


	public Dao getDao() {
		return dao;
	}


	public void setDao(Dao dao) {
		this.dao = dao;
	}
}
